/*
 This file 'HashMap' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 16:15
 */

define(function (require, exports, module) {

	/**
	 * Firebird.lang.HashMap
	 * @class Firebird.lang.HashMap
	 * @extends Firebird.Model
	 */
	Firebird.define("Firebird.lang.HashMap", {
		extend: "Firebird.Model",
		config: {
			value: null
		},
		/**
		 * _length
		 */
		_length: 0,
		/**
		 * _maxLength
		 */
		_maxLength: Number.MAX_VALUE,
		/**
		 * _container
		 */
		_container: null,
		/**
		 *
		 * @private
		 */
		_init: function (c) {
			var me = this;
			me._container = new Object();

			$.each(me.getValue() || {}, function (k, v) {
				me.put(k, v)
			});
		},
		applyValue: function (value) {
			return value;
		},
		/**
		 * size
		 * @returns {number}
		 */
		size: function () {
			return this._length;
		},
		/**
		 * put
		 * @param key
		 * @param value
		 * @returns {Firebird.HashMap}
		 */
		put: function (key, value, plain) {
			if (!(key in this._container)) {
				this._length++;
			}
			this._container[key] = value;
			this.onChange.apply(this, ["put", key, this.get(key)]);
			return this;
		},
		/**
		 * 升级
		 * @param key
		 * @param value
		 * @returns {Firebird.lang.HashMap}
		 */
		update: function (key, value) {
			$.extend(true, this._container[key], value);
			this.onChange.apply(this, ["update", key, this.get(key)]);
			return this;
		},
		/**
		 * get
		 * @param key
		 * @returns {*}
		 */
		get: function (key) {
			return key in this._container ? this._container[key] : null;
		},
		/**
		 * keys
		 * @returns {Array}
		 */
		keys: function () {
			var keys = [];
			for (var key in this._container) {
				keys.push(key);
			}
			return keys;
		},
		/**
		 * values
		 * @returns {Array}
		 */
		values: function () {
			var values = [];
			for (var key in this._container) {
				values.push(this._container[key]);
			}
			return values;
		},
		/**
		 * remove
		 * @param key
		 * @returns {Firebird.HashMap}
		 */
		remove: function (key) {
			var removed = $.extend(true, {}, this._container[key]);
			if (key in this._container && (delete this._container[key])) {
				this._length--;
			}
			this.onChange.apply(this, ["remove", key, removed]);
			return this;
		},
		/**
		 * clear
		 * @returns {Firebird.HashMap}
		 */
		clear: function () {
			this._container = new Object();
			this._length = 0;
			this.onChange.apply(this, ["clear"]);
			return this;
		},
		/**
		 * 遍历元素
		 * @param callback
		 */
		each: function (callback) {
			var i = 0;
			for (var key in this._container) {
				if (callback && callback(key, this._container[key], i++) === false) {
					break;
				}
			}
		},
		/**
		 * getMap
		 * @returns {*}
		 */
		getMap: function () {
			return this._container;
		},
		/**
		 * toString
		 * @returns {string}
		 */
		toString: function () {
			return [this.$className, "[", decodeURIComponent($.param(this.getMap()).replace(/\&/g, ", ")), "]"].join(" ");
		},
		/**
		 * 当Map发生变化时的将会触发的事件
		 * @param type 事件类型
		 * @param key
		 * @param value
		 */
		onChange: function (type, key, value) {

		}
	});

	module.exports = Firebird.lang.HashMap;

});